import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:test01/okhttp_test/pojo/attention_pojo.dart';
import 'package:test01/okhttp_test/util/request_http.dart';

/*
*
*
* 我的---粉丝
*
* */
class FansPage extends StatefulWidget {
  const FansPage({Key key}) : super(key: key);

  @override
  _FansPageState createState() => _FansPageState();
}

class _FansPageState extends State<FansPage> {
  Attention attentions=Attention();
  //列表数据
  List<AttentionInformation> attentionsList=[];



  ///粉丝列表
  Widget fansitem(int i){
    return Container(
      height: 48,
      width: MediaQuery.of(context).size.width,
      margin: EdgeInsets.only(top: 10),
      child: Row(
        children: <Widget>[
          Container(
            height: 38,
            width: 38,
            margin: EdgeInsets.only(left: 16),
            child: ClipOval(
              child: Image.network(
                attentionsList[i].avatarUri,
                fit: BoxFit.cover,
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.only(left: 12),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container(
                  width: MediaQuery.of(context).size.width-66-88,
                  child: Text(attentionsList[i].nickName,style: TextStyle(fontSize: 15,color: Colors.black),maxLines: 1,overflow: TextOverflow.ellipsis,),
                ),
                (attentionsList[i].description!=null)?Container(
                  width: MediaQuery.of(context).size.width-66-88,
                  child: Text(attentionsList[i].description,style: TextStyle(fontSize: 12,color:Color.fromRGBO(153, 153, 153, 1)),maxLines: 1,overflow: TextOverflow.ellipsis,),
                ):Container(),
              ],
            ),
          ),
          Spacer(),
          attentionsList[i].relation==3?Container(
            width: 56,
            height: 24.5,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: Color.fromRGBO(255, 255, 255, 1),
              borderRadius: BorderRadius.circular(14),
              border: Border.all(width: 0.5,color: Color.fromRGBO(204, 204, 204, 1))
            ),
            child: Text("已关注",style:TextStyle(fontSize: 12,color: Color.fromRGBO(204, 204, 204, 1)),),
          ):GestureDetector(
            onTap: (){
              print("===================粉丝添加关注=============");
              addMyFollow(attentionsList[i].uid);
            },
            child: Container(
              width: 56,
              height: 24.5,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Color.fromRGBO(17, 17, 17, 1),
                borderRadius: BorderRadius.circular(14),
              ),
              child: Text("关注",style:TextStyle(fontSize: 12,color: Color.fromRGBO(255, 255, 255, 1)),),
            ),
          ),
          SizedBox(
            width: 16,
          )
        ],
      ),
    );
  }


  @override
  void initState() {
    attentionApi();
    super.initState();
  }


  ///调用关注用户列表接口----粉丝
  attentionApi() async{
    Attention attention=await QueryFollowingList(size:10,interface:"/appuser/web/user/follow/QueryFollowingList");
    for(int i=0;i<attention.list.length;i++){
      if(attention.list[i].relation==2||attention.list[i].relation==3){
        attentionsList.add(attention.list[i]);
      }
    }
    attention=attentions;
    setState(() {
    });
  }

  ///关注粉丝
  addMyFollow(int uid) async{
    bool sult=await addFollow(targetId:uid,interface:"/appuser/web/user/follow/addFollow");
    if(sult){
      Fluttertoast.showToast(msg:"关注成功！",toastLength: Toast.LENGTH_SHORT,gravity: ToastGravity.BOTTOM,
          textColor: Colors.white,
          fontSize: 14.0);
      setState(() {
        attentionsList.clear();
        attentionApi();
      });
    }
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:AppBar(
        ///去掉APPBar的箭头
        automaticallyImplyLeading: false,
        backgroundColor: Colors.white,
        elevation: 0,
        title: Row(
          children: <Widget>[
            GestureDetector(
              onTap: (){
                Navigator.pop(context);
              },
              child: Container(
                // margin: EdgeInsets.only(left: 16),
                child: Icon(Icons.arrow_back_ios,color: Colors.black,size: 24,),
              ),
            ),
            Container(
              width: MediaQuery.of(context).size.width-72-72,
              alignment: Alignment.center,
              margin: EdgeInsets.only(left: 32),
              child: Text(
                "我的粉丝",
                style: TextStyle(fontSize: 18,color: Color.fromRGBO(17, 17, 17, 1)),
              ),
            )
          ],
        ),
      ),
      body: SingleChildScrollView(
        child:Column(
          children: <Widget>[
            attentionsList.length!=0?
            Container(
              margin: EdgeInsets.only(top: 8),
              height: MediaQuery.of(context).size.height-18-48-48,
              child:  ListView.builder(
                reverse: false,
                itemBuilder: (BuildContext context, int i) {
                  return fansitem(i);
                },
                itemCount: attentionsList.length,
              ),
            ):
            Container(
            )
          ],
        ),
      ),
    );
  }
}

